package org.ohos.sample.webview.home.slice;

import com.utalk.kernel.logger.L;
import com.utalk.kernel.util.StringCompat;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.webengine.WebView;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.multimodalinput.event.KeyEvent;
import org.ohos.sample.cordova.SystemWebViewEngine;
import org.ohos.sample.cordova.WebViewEngine;
import org.ohos.sample.cordova.WebViewInterface;
import org.ohos.sample.webview.ResourceTable;
import org.ohos.sample.webview.home.WebAbility;

import java.util.Optional;

/**
 *
 *  加载资源文件 dataability://org.ohos.sample.data.DataAbility
 *  final String URL_LOCAL = "dataability://org.ohos.sample.data.DataAbility/resources/rawfile/web/index.html";
 *  webViewEngine.load(URL_LOCAL);
 *
 *  加载资源文件 resources/rawfile/example.html
 *  webViewEngine.load(loadUrl);
 *
 *  加载本地文件 /data/data/com.example.dataability/files/example.html
 *  webViewEngine.load("https://example.com/local/example.html");
 *
 *  加载网址
 *  webViewEngine.load("http://elemefe.github.io/mint-ui/#/");
 *
 *
 *
 */
public class WebAbilitySlice extends AbilitySlice implements WebViewInterface {

    private WebViewEngine webViewEngine;
    private String loadUrl;

    private WebViewInterface webViewInterface;
//    private Component viewLoad;
    private Component progressBarLayout;
    private ProgressBar mProgressBar;
    private String TAG="WebAbilitySlice";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        super.setUIContent(ResourceTable.Layout_ability_webview);
        Optional<Display>
                display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
        Point pt = new Point();
        display.get().getSize(pt);
        WebView mWebView = (WebView) findComponentById(ResourceTable.Id_webview);
        progressBarLayout = findComponentById(ResourceTable.Id_webview_load_progress);
        mProgressBar = (ProgressBar) findComponentById(ResourceTable.Id_webview_view_progressbar);
        L.i(TAG,StringCompat.string("x=",pt.getPointXToInt()," y=",pt.getPointYToInt()));
//        DependentLayout.LayoutConfig layoutConfig = (DependentLayout.LayoutConfig) mWebView.getLayoutConfig();
//        layoutConfig.height = pt.getPointYToInt();
//        layoutConfig.width = pt.getPointXToInt();
//        mWebView.setLayoutConfig(layoutConfig);

        if(intent.hasParameter(WebAbility.KEY_URL)){
            loadUrl = intent.getStringParam(WebAbility.KEY_URL);
        }else{
            loadUrl="https://example.com/rawfile/web/index.html";
        }
        webViewInterface = this;
        if(webViewEngine==null){
            webViewEngine = new SystemWebViewEngine(getApplicationContext(),webViewInterface);
        }
//        mStackLayout.moveChildToFront(viewLoad);
        webViewEngine.init(mWebView);
        mWebView.load(loadUrl);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
        L.i(TAG,StringCompat.string("onKeyUp->keyCode=",keyCode));
        return super.onKeyUp(keyCode, keyEvent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        L.i(TAG,StringCompat.string("onKeyDown->keyCode=",keyCode));
        if(keyCode==KeyEvent.KEY_BACK){
            boolean canGoBack = webViewEngine.canGoBack();
            L.i(TAG,StringCompat.string("onKeyDown->WebViewEngine canGoBack=",canGoBack));
            if(canGoBack){
                webViewEngine.goBack();
                return  true;
            }
        }
        return super.onKeyDown(keyCode, keyEvent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(webViewEngine!=null){
            webViewEngine.destroy();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void showWebViewLoading() {
        L.i(TAG,"showWebViewLoading");
        if(progressBarLayout!=null){
            progressBarLayout.setVisibility(Component.VISIBLE);
        }
    }

    @Override
    public void hideWebViewLoading() {
        L.i(TAG,"hideWebViewLoading");
        if(progressBarLayout!=null){
            progressBarLayout.setVisibility(Component.HIDE);
        }
    }

    @Override
    public void onWebViewPageFinished() {
        L.i(TAG,"onWebViewPageFinished");
    }
    @Override
    public void onWebViewProgressUpdated(int newProgress) {
        L.i(TAG, StringCompat.string("onWebViewProgressUpdated=",newProgress));
        if(mProgressBar!=null){
            mProgressBar.setProgressValue(newProgress);
        }
    }
}